import js from '@eslint/js';
import tseslint from 'typescript-eslint';
import reactPlugin from 'eslint-plugin-react';

// 检测是否已安装TypeScript相关依赖
const hasTypeScript = () => {
  try {
    require.resolve('@typescript-eslint/eslint-plugin');
    return true;
  } catch (e) {
    return false;
  }
};

// 基础配置
const baseConfig = [
  js.configs.recommended,
  {
    files: ['**/*.{js,jsx,ts,tsx}'],
    languageOptions: {
      ecmaVersion: 'latest',
      sourceType: 'module',
      jsx: true,
    },
    plugins: {
      react: reactPlugin,
    },
    rules: {
      'no-unused-vars': 'error', // 基础JavaScript规则
    },
    settings: {
      react: {
        version: 'detect',
      },
    },
  },
];

// 如果有TypeScript依赖，添加TypeScript特定配置
if (hasTypeScript()) {
  baseConfig.push(...tseslint.configs.recommended);
  baseConfig.push({
    files: ['**/*.{ts,tsx}'],
    languageOptions: {
      parser: tseslint.parser,
      parserOptions: {
        project: './tsconfig.json',
      },
    },
    rules: {
      '@typescript-eslint/no-unused-vars': 'error', // TypeScript特定规则
    },
  });
}

export default baseConfig;